package cn.edu.scau.cmi.chenjiale.integration.utils;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * 数据库工具类
 * 
 * @author chen.jiale
 * @date 2019/05/18
 *
 */
public class DBUtils {
	private static SessionFactory sessionFactory;

	/**
	 * 加载hibernate配置信息
	 */
	static {
		Configuration configuration = new Configuration();
		configuration.configure();
		sessionFactory = configuration.buildSessionFactory();
	}

	public static Session session() {
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		return session;
	}
	
	/**
	 * 提交事务并关闭session
	 * 
	 * @param session
	 */
	public static void commitTransactionAndClose(Session session) {
		session.getTransaction().commit();
		session.close();
	}
}
